package com.chuangyang.fixboxmaster.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.ui.fragment.PhotoReviewFragment;

import java.util.ArrayList;

/**
 * Created by vanshu on 15/4/13 15:30.
 */
public class PhotoViewActivity extends BaseActivity {
    private ArrayList<String> photo_url=new ArrayList<>();
    private static final int DEFAULT_POSITION=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        photo_url= getIntent().getStringArrayListExtra("photo_url");
        int currentPosition=getIntent().getIntExtra("currentPosition", DEFAULT_POSITION);
        setContentView(R.layout.activity_photo_view);
        ViewPager mViewPager = (ViewPager)findViewById(R.id.view_pager);
        ViewPagerAdapter mAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        if(currentPosition==DEFAULT_POSITION){
            finish();
        }
        mViewPager.setOffscreenPageLimit(photo_url.size());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(currentPosition);
    }



    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            PhotoReviewFragment photoReviewFragment = new PhotoReviewFragment();
            Bundle bundle = new Bundle();
            bundle.putString("photo_url", photo_url.get(i));
            photoReviewFragment.setArguments(bundle);
            return photoReviewFragment;
        }

        @Override
        public int getCount() {
            return photo_url.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }
    }


}
